﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CLEX.Service.DataContracts;

namespace CLEX.Client.Metro.ViewModel.Home
{
    public class DatabaseDeleteVM : VMBase
    {
        private readonly IFacade facade;
        private readonly IPopupService popup;
        private readonly DatabaseData database;
        private readonly RelayCommand delete;
        private readonly RelayCommand cancel;

        public string ConnectionString
        {
            get { return database.ConnectionString; }
        }

        public ICommand Delete
        {
            get { return delete; }
        }

        public ICommand Cancel
        {
            get { return cancel; }
        }

        public DatabaseDeleteVM(IFacade facade, IPopupService popup, DatabaseData database)
        {
            this.facade = facade;
            this.popup = popup;
            this.database = database;
            this.delete = new RelayCommand(DoDelete);
            this.cancel = new RelayCommand(DoCancel);
        }

        private void DoDelete()
        {
            try
            {
                facade.DeleteDatabase(database.DatabaseId);
            }
            catch (Exception ex)
            {
                facade.ShowUnexpectedError("Unable to delete the database.", ex.Message);
                return;
            }
            popup.ClosePopup();
        }

        private void DoCancel()
        {
            popup.ClosePopup();
        }
    }
}
